/**
 * FreeZzaphPluginFactory.java
 * 
 * FreeZzaph is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * FreeZzaph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.
 */
package freezzaph;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;


import freezzaph.classloader.PluginClassLoader;
import freezzaph.exceptions.PluginException;
import freezzaph.plugins.FreeZzaphPlugin;

/**
 * Class for loading plugins.
 * 
 * @author FreeZzaph
 */
final class FreeZzaphPluginFactory {
	
	/**
	 * FileFilter that accepts files that match
	 * *.fzzaph.
	 * 
	 * @author FreeZzaph
	 */
	private final class FZzPluginFilter implements FileFilter {

		/**
		 * I think, therefore I am.
		 */
		public FZzPluginFilter() {/**/}

		/*
		 * (non-Javadoc)
		 * @see java.io.FileFilter#accept(java.io.File)
		 */
		public boolean accept(File pathname) {
			String filename = pathname.getName(); 
			if (filename.length() > 7 && filename.substring(filename.length() - 7, filename.length()).equals(".fzzaph")) {
				return true;
			}
			return false;
		}
	}

	private HashMap<String, FreeZzaphPlugin> pluginList = new HashMap<String, FreeZzaphPlugin>();
	
	public void loadPlugins() throws PluginException {
		
		File pluginFolder = new File("./plugins");
		
		// For any file that matches "*.fzzaph"...
		try {
			for (File pluginFile : pluginFolder.listFiles(new FZzPluginFilter())) {
				try {
					// Load the file as a plugin
					PluginClassLoader pcl = new PluginClassLoader(pluginFile.getCanonicalPath());
					Class<?> pluginClass = pcl.loadPluginClass();
					FreeZzaphPlugin gcp = (FreeZzaphPlugin) pluginClass.newInstance();
					
					String pluginName = gcp.getPluginName(); 
					if (!pluginList.containsKey(pluginName)) {
						pluginList.put(pluginName, gcp);
					} else {
						System.err.printf("Conflicting plugin names, tried loading plugin with name '%s', but one already existed with such a name.", pluginName);
					}
				} catch (Exception e) {
					System.err.printf("Failed loading plugin from %s\n", pluginFile.getName());
					continue;
				}
			}
		} catch (SecurityException e) {
			throw new PluginException("Cannot access plugin directory\n");
		}
		
	}

	/**
	 * Get all the loaded plugins
	 * 
	 * @return HashMap containing all the loaded plugins
	 */
	public HashMap<String, FreeZzaphPlugin> getPluginList() {
		return pluginList;
	}

}
